import eventObserver from './zh_jdgjb/utils/eventObserverClass'
App({
	globalData: {
		hasRegister: false, // 当前用户是否已经注册会员
		userMinaInfo: {
			is_mobile: 0,
			openid: '',
			session_key: '',
			unionid: '', // 开放平台下唯一ID
			user_id: '' // 用户Id
		}, // 微信小程序相关的用户信息，如：openid、unionid等
		userId: 0, // 用户Id
		userInfo: null, // 用户信息
		eventObserver, // 单例eventBus
		payAppId: 'wx9a95331605fc5eb5', // 易宝支付小程序的APPID
		appId: '', // 小程序APPID
		statusBarHeight: 0, // 状态栏高度
		navBarHeight: 0, // 导航栏高度
		menuRight: 0, // 胶囊距右方间距（方保持左、右间距一致）
		menuTop: 0, // 胶囊距底部间距（保持底部间距一致）
		menuHeight: 0, // 胶囊高度（自定义内容可与胶囊高度保证一致）
		screenWidth: 0, // 手机屏幕宽度
		screenHeight: 0 // 手机屏幕高度
	},
	util: require('we7/resource/js/util.js'),
	siteInfo: require('siteinfo.js'),
	onLaunch(options) {
		this.initData(options)
	},
	onShow(query) {
		console.log('小程序接收的参数：', query)
		this.globalData.eventObserver.publish('refreshOrderStatus', query, false)
	},
	/**
	 * @function 初始化参数
	 * @param {*} options 其他小程序传递来的参数
	 */
	initData(options) {
		this.initSystemInfo()
		const { userId } = options.query
		if (userId) {
			this.globalData.userId = userId
		}
		console.log('其他小程序传递来的参数：', options, this.globalData.userId)
	},
	/**
	 * @function 初始化导航栏参数
	 */
	initSystemInfo() {
		// 获取系统信息
		const systemInfo = wx.getSystemInfoSync()
		// 胶囊按钮位置信息
		const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
		// 导航栏高度 = 状态栏高度 + 44
		this.globalData.statusBarHeight = systemInfo.statusBarHeight
		this.globalData.navBarHeight = systemInfo.statusBarHeight + 44
		this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right
		this.globalData.menuTop = menuButtonInfo.top
		this.globalData.menuHeight = menuButtonInfo.height
		this.globalData.screenWidth = systemInfo.screenWidth
		this.globalData.screenHeight = systemInfo.screenHeight
	},
	/**
	 * @function 获取当前小程序的appId
	 */
	getMinaAppId() {
		if (this.globalData.appId) {
			return this.globalData.appId
		}
		this.globalData.appId = wx.getAccountInfoSync().miniProgram.appId
		return this.globalData.appId
	},
	/**
	 * @function 获取用户信息,用于其他页面主动调用
	 */
	async getUserInfo(cb) {
		this.getMinaAppId()
		const code = await this.getLoginCode()
		const userMinaInfo = await this.wxLogin(code)
		const userInfo = await this.getUserInformation(userMinaInfo.user_id)
		console.log(' 获取到的参数：', code, userInfo, userMinaInfo)
		this.globalData.userMinaInfo = userMinaInfo
		this.globalData.userInfo = userInfo
		this.globalData.userId = userMinaInfo.user_id
		this.globalData.hasRegister = userMinaInfo.is_mobile !== 0
		wx.setStorageSync('userInfo', userInfo)
		cb && cb(userInfo)
		return userInfo
	},
	/**
	 * @function 获取登录Code
	 */
	getLoginCode() {
		return new Promise((resolve, reject) => {
			wx.login({
				success: res => {
					resolve(res.code)
				}
			})
		})
	},
	/**
	 * @function 获取用户openId
	 */
	wxLogin(code) {
		return new Promise((resolve, reject) => {
			this.util.request({
				url: 'index/wxLogin',
				method: 'POST',
				cachetime: '0',
				testapi: this.siteInfo.siteroot_web,
				data: {
					code,
					appid: this.globalData.appId
				},
				success: res => {
					console.log('wxLogin:', res)
					const { code, data } = res.data
					if (code === 0) {
						// if (data.is_mobile === 0) {
						// 	wx.navigateTo({
						// 		url: `/zh_jdgjb/pages/register/register`
						// 	})
						// }
						resolve(data)
					}
				}
			})
		})
	},
	/**
	 * @function 获取用户详细信息
	 * @param {*} openId
	 */
	getUserInformation(userId) {
		console.log('获取用户详细信息：', userId)
		if (!userId) {
			return
		}
		return new Promise((resolve, reject) => {
			this.util.request({
				url: 'my/getUserInfo',
				cachetime: '0',
				data: {
					user_id: userId
				},
				testapi: this.siteInfo.siteroot_web,
				success: res => {
					resolve(res.data.data)
				}
			})
		})
	},
	getUrl: function (e) {
		var t = this.globalData.url
		e.setData({
			url: t
		})
		var n = this,
			m = e
		t ||
			n.util.request({
				url: 'entry/wxapp/attachurl',
				success: function (t) {
					// n.globalData.url = t.data, n.getUrl(e);
					n.globalData.url = t.data
					m.setData({
						url: n.globalData.url
					})
				}
			})
	},
	getSystem: function (e) {
		var t = this.globalData.color,
			n = this.globalData.system
		t &&
			e.setData({
				color: t,
				platform: n
			})
		var a = this
		t ||
			a.util.request({
				url: 'entry/wxapp/GetSystem',
				success: function (t) {
					console.log('查询到的导航栏配置', t),
						-1 < t.data.jd_custom.indexOf('查询') &&
							(t.data.jd_custom = t.data.jd_custom.replace('查询', '')),
						(a.globalData.color = t.data.color),
						(a.globalData.system = t.data),
						a.getSystem(e)
				}
			})
	},
	max: function (t) {
		t = t[0]
		for (var e = t.length, n = 1; n < e; n++) t[n] > t && (t = t[n])
		return t
	},
	today_time: function (t) {
		var e = new Date(),
			n = e.getMonth() + 1,
			a = e.getDate()
		return (
			1 <= n && n <= 9 && (n = '0' + n),
			0 <= a && a <= 9 && (a = '0' + a),
			e.getFullYear() +
				'-' +
				n +
				'-' +
				a +
				' ' +
				e.getHours() +
				':' +
				e.getMinutes() +
				':' +
				e.getSeconds()
		)
	},
	hours_time: function (t, e) {
		var n = new Date(t.replace('-', '/')),
			a = 60 * (e = Number(e))
		return (
			n.setMinutes(n.getMinutes() + a, n.getSeconds(), 0),
			n.getFullYear() +
				'-' +
				(n.getMonth() + 1) +
				'-' +
				n.getDate() +
				' ' +
				n.getHours() +
				':' +
				n.getMinutes() +
				':' +
				n.getSeconds()
		)
	},
	today: function () {
		var t = new Date(),
			e = t.getFullYear(),
			n = t.getMonth() + 1,
			a = t.getDate()
		return (
			1 <= n && n <= 9 && (n = '0' + n),
			0 <= a && a <= 9 && (a = '0' + a),
			e + '-' + n + '-' + a
		)
	},
	ormatDate: function (t) {
		var e = new Date(1e3 * t)
		return (
			e.getFullYear() +
			'-' +
			n(e.getMonth() + 1, 2) +
			'-' +
			n(e.getDate(), 2) +
			' ' +
			n(e.getHours(), 2) +
			':' +
			n(e.getMinutes(), 2) +
			':' +
			n(e.getSeconds(), 2)
		)
		function n(t, e) {
			for (var n = '' + t, a = n.length, o = '', r = e; r-- > a; ) o += '0'
			return o + n
		}
	},
	ormatDate2: function (t) {
		var e = new Date(1e3 * t)
		return (
			n(e.getMonth() + 1, 2) +
			'月' +
			n(e.getDate(), 2) +
			'日 ' +
			n(e.getHours(), 2) +
			':' +
			n(e.getMinutes(), 2)
		)
		function n(t, e) {
			for (var n = '' + t, a = n.length, o = '', r = e; r-- > a; ) o += '0'
			return o + n
		}
	},
	sort_price_Reverse: function (t, e) {
		return t.zd_money - e.zd_money
	},
	sort_price_order: function (t, e) {
		return e.zd_money - t.zd_money
	},
	sort_num_order: function (t, e) {
		return t - e
	},
	sort_distance_order: function (t, e) {
		return t.distance - e.distance
	},
	sort_distance_Reverse: function (t, e) {
		return e.distance - t.distance
	},
	time_title: function (t, e) {
		return (
			!(e <= t) ||
			(wx.showToast({
				title: '退房日期需大于入住日期',
				icon: 'none'
			}),
			!1)
		)
	},
	getTime2Time: function (t, e) {
		;(t = t), (e = e)
		return ((t = Date.parse(t) / 1e3) - (e = Date.parse(e) / 1e3)) / 86400
	},
	getSys: function () {
		var e = this
		wx.getSystemInfo({
			success: function (t) {
				;(e.globalData.sysInfo = t),
					(e.globalData.windowW = t.windowWidth),
					(e.globalData.windowH = t.windowHeight)
			}
		})
	}
})
